from django.db import models
from apps.system.models import Upload
from utils.exceptions import CustomError


class Worker(models.Model):
    name = models.CharField(verbose_name='姓名', max_length=50)
    face = models.ForeignKey(Upload, verbose_name='头像地址', on_delete=models.PROTECT, blank=True, null=True)
    age = models.IntegerField(verbose_name='年龄')
    skills = models.CharField(verbose_name='技能', max_length=250)
    start = models.IntegerField(verbose_name='评分', default=5)
    work_age = models.IntegerField(verbose_name='从业经验,工龄', default=1)
    case_count = models.IntegerField(verbose_name='案例个数', default=0)
    mobile = models.CharField(verbose_name='手机号', max_length=11)
    id_number = models.CharField(verbose_name='身份证号', max_length=50, null=True)
    status = models.BooleanField(verbose_name='状态', default=True)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)

    class Meta:
        db_table = 'worker'
        verbose_name = '工人'
        ordering = ('-id',)

    @staticmethod
    def get_instance_by_id(id):
        try:
            id = int(id)
        except:
            raise CustomError('无效的装修流程id')

        instance = Worker.objects.filter(pk=id).first()
        if not instance:
            raise CustomError('未找到对应的装修流程实例')
        return instance
